<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\Guest as GuestModel;
use app\common\Logs;
use think\Db;
use app\api\model\Single as SingleModel;
use app\api\model\TeamIncome as TeamIncomeModel;
class Guest extends BaseController{
    public function __construct(GuestModel $guestModel){
        parent::__construct();
        $this->model = $guestModel;
    }
    public function index(){
        $params = $this->request->get();       
        $data = $this->model->allowField(true)->guestList($params);
        return $data;
    }
    public function info(){
        $params = $this->request->get();
        $data = $this->model->allowField(true)->where('id',$params['id'])->select();
        return $data;
    }
    public function check(){       
        $params = $this->request->get(); 
        $data = $this->model->allowField(true)->where('guest_name',trim($params['guest_name']))->select();        
        return $data;
    }
    public function csv(){
        $params = $this->request->get();
        $data = $this->model->allowField(true)->allList($params);
        return $data;
    }
    //添加
    public function create(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);    
        $params['guest_name']=trim($params['guest_name']);
        $data = $this->model->allowField(true)->isUpdate(false)->save($params);
        if($data){
            Logs::add("新增客人记录",$operator_id,$this->model->guest_name, null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //更新
    public function update(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true); 
        $old = $this->model->where("id",$params['id'])->find();  
        $params['guest_name']=trim($params['guest_name']);   
        $data = $this->model->allowField(true)->isUpdate(true)->save($params);
        if($data){
            Logs::add("修改客人记录",$operator_id,$old['guest_name'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $old = $this->model->where("id",$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除客人记录",$params['operator_id'],$old['guest_name'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }  
        return $data;
    }

    public function clear(){
        /*
        $list = Db::query('select * from lt_guest');
        foreach($list as $item){
            if($item['phone']==null){
                $single=Db::name('guest')->where('id',$item['id'])->update(['phone'=>'']);
            }
        }
        */
        $list = Db::query('select guest_name,phone,count(*) as num from lt_guest GROUP BY guest_name,phone HAVING num>1');
       
        foreach($list as $item){           
            $guest = Db::query("select id from lt_guest where guest_name='{$item['guest_name']}' and phone='{$item['phone']}' order by id desc");  
            
            $ids=[];        
            if($guest){                
                foreach($guest as $g){
                    array_push($ids,intval($g['id']));              
                }          
                
                //$single = Db::query("select id,guest_id from lt_single where guest_id in ({$ids})");
                //$single=Db::name('single')->field('id,guest_id')->where('guest_id','in',$ids)->select();
               $single=Db::name('single')->where('guest_id','in',$ids)->update(['guest_id'=>$guest[0]['id']]);
                //$team = Db::query("select id,guest_id from lt_team_income where guest_id in ({$ids})");
                //$single=Db::name('team_income')->field('id,guest_id')->where('guest_id','in',$ids)->select();
               $single=Db::name('team_income')->where('guest_id','in',$ids)->update(['guest_id'=>$guest[0]['id']]);  
                $single=Db::name('guest')->where(['id'=>['in',$ids]])->where(['id'=>['<>',$guest[0]['id']]])->delete();                 
            }
                     
        }
       
    }
}
